import Vue from "vue";
import VueRouter from "vue-router";
const Login = () =>
  import(/* webpackChunkName: 'ImportFuncDemo' */ "../views/Login.vue");

const Home = () =>
  import(/* webpackChunkName: 'ImportFuncDemo' */ "../views/Home.vue");

const Welcome = () =>
  import(/* webpackChunkName: 'ImportFuncDemo' */ "../components/Welcome.vue");

const Users = () =>
  import(
    /* webpackChunkName: 'ImportFuncDemo' */ "../components/users/users.vue"
  );

const Roles = () =>
  import(
    /* webpackChunkName: 'ImportFuncDemo' */ "../components/power/Roles.vue"
  );

const Rights = () =>
  import(
    /* webpackChunkName: 'ImportFuncDemo' */ "../components/power/Rights.vue"
  );

const Goods = () =>
  import(
    /* webpackChunkName: 'ImportFuncDemo' */ "../components/goods/List.vue"
  );

const Params = () =>
  import(
    /* webpackChunkName: 'ImportFuncDemo' */ "../components/goods/Params.vue"
  );

const Categories = () =>
  import(
    /* webpackChunkName: 'ImportFuncDemo' */ "../components/goods/Categories.vue"
  );

const Order = () =>
  import(
    /* webpackChunkName: 'ImportFuncDemo' */ "../components/order/Order.vue"
  );

const Report = () =>
  import(
    /* webpackChunkName: 'ImportFuncDemo' */ "../components/report/Report.vue"
  );

const View = () =>
  import(/* webpackChunkName: 'ImportFuncDemo' */ "../views/view.vue");

const Add = () =>
  import(
    /* webpackChunkName: 'ImportFuncDemo' */ "../components/goods/Add.vue"
  );

const FullPage = () =>
  import(/* webpackChunkName: 'ImportFuncDemo' */ "../views/fullPage.vue");

const Animation = () =>
  import(/* webpackChunkName: 'ImportFuncDemo' */ "../views/Animation.vue");

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/view",
    component: View
  },
  {
    path: "/fullPage",
    component: FullPage
  },
  {
    path: "/animation",
    component: Animation
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: Welcome
      },
      {
        path: "/users",
        component: Users
      },
      {
        path: "/roles",
        component: Roles
      },
      {
        path: "/rights",
        component: Rights
      },
      {
        path: "/goods",
        component: Goods
      },
      {
        path: "/goods/add",
        component: Add
      },
      {
        path: "/params",
        component: Params
      },
      {
        path: "/categories",
        component: Categories
      },
      {
        path: "/orders",
        component: Order
      },
      {
        path: "/reports",
        component: Report
      }
    ]
  }
];

const router = new VueRouter({
  routes
});

// 挂载路由导航守卫
router.beforeEach((to, form, next) => {
  if (to.path === "/login") {
    return next();
  } else {
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) {
      return next("/login");
    } else {
      return next();
    }
  }
});

export default router;
